<?php

class Libs_Bootstrap {

    public function __construct() {
        $this->delegate();
    }
    
    public function parseUrl(&$controller, &$action, &$param = NULL){
        if(isset($_GET['url'])){
            $arr_url = explode('/', rtrim($_GET['url'],'/'));
            if(file_exists('controllers/'.$arr_url[0].'.php')){
                $controller = $arr_url[0];
                array_shift($arr_url);
            }else{
               $controller = 'Index';
               $action = 'index';
            }
            if(isset($arr_url[0])){
                $class = 'Controllers_'.$controller;
                if(is_callable(array($class,$arr_url[0]))){
                    $action = $arr_url[0];
                    array_shift($arr_url);
                }else{
                    $action = 'index';
                }
                if(count($arr_url) > 0){
                    $param = $arr_url;
                }
            }else{
                $action = 'index';
            }
        }else{
            $controller = 'Index';
            $action = 'index';
        }
    }
    
    public function delegate(){
        $this->parseUrl($controller, $action, $param);
        $class = "Controllers_".$controller;  
        $controller = new $class();
        $controller->$action($param);
    }

}

